

// source idl: com.erp.test.ao.idl.HelloworldAo.java

#ifndef HELLOWORLDAO_STUB_4_WEB_H
#define HELLOWORLDAO_STUB_4_WEB_H

#include <string>
#include <sstream>
#include "app_platform/cmd_obj.h"
#include "app_platform/cgi_stub_cntl.h"
#include "c2cent/base/basestub4web.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_serializable.h"
#include "app_platform/intf_service_object.h"
#include "intf_helloworldao.h"

namespace com
{
namespace erp
{
namespace test
{
namespace ao
{
	#define HELLOWORLDAO_STUB4WEB_VERSION "COM_ERP_TEST_AO___HELLOWORLDAO__STUB4WEB_VERSION__1.0 2013-09-30 17:49:46"
    const std::string HELLOWORLDAO_STUB4WEB_NOUSE = HELLOWORLDAO_STUB4WEB_VERSION;

	const uint32_t HELLOWORLDAO_SAYHELLO_REQ_CMD_ID = 0xc0281801; /// 请求命令号
	const uint32_t HELLOWORLDAO_SAYHELLO_RESP_CMD_ID = 0xc0288801; /// 回应命令号
	


	const uint16_t HELLOWORLDAO_MAX_PROTOCAL_VERSION = 2; //>平台支持的最大协议版本号

///////////////////////////// STRUCTS /////////////////////////////////////////////////
	/**
	 *
 	 * 
	 *
	 *
 	 * @date 2013-09-30 17:49:47
 	 * @version 0
	 */	
	struct CSayHelloReq : public IServiceObject, public ISerializableXML
	{
		std::string NickName;
		uint32_t Age;
		
	    virtual uint32_t getCmdId() const { return HELLOWORLDAO_SAYHELLO_REQ_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
			//检查协议的版本号
		    //if(bs.getVersion() > HELLOWORLDAO_MAX_PROTOCAL_VERSION)
		    //return false;
			bs & NickName;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	
	/**
	 *
 	 * 
	 *
 	 * @date 2013-09-30 17:49:47
 	 * @version 0
	 */	
	struct CSayHelloResp : public IServiceObject, public ISerializableXML
	{
		uint32_t dwResult;
		uint32_t Age;
	
	    virtual uint32_t getCmdId() const { return HELLOWORLDAO_SAYHELLO_RESP_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
		    bs & dwResult;
			bs & Age;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	class CHelloworldAoStub4Web : public c2cent::CBaseStub4Web, public IHelloworldAo	{
		public:
			uint32_t SayHello(
				const CCntlInfo& rCntlInfo,

				const std::string& NickName,
				uint32_t& Age
			);
	};
	
	/**
	 *
 	 * say hello
	 *
		 *@param String NickName: 输入昵称 				
		 *@param uint32_t Age: 返回年龄信息 		
     *
     *@return uint32_t : 0(成功) 其它失败
	 */	
	inline uint32_t CHelloworldAoStub4Web::SayHello(
				const CCntlInfo& rCntlInfo,

				const std::string& NickName,
				uint32_t& Age
	)
	{
		 CSayHelloReq oReq;
			oReq.NickName = NickName;
			oReq.Age = Age;
		 
		// CCGI_STUB_CNTL->setRouteKey(uint32_t routeKey); 没有设置routeKey  
		
	    CCGI_STUB_CNTL->setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			CCGI_STUB_CNTL->setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
		if((iRetCode = CCGI_STUB_CNTL->putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed: ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(HELLOWORLDAO_SAYHELLO_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << HELLOWORLDAO_SAYHELLO_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CSayHelloResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
		    
			Age  = oResp.Age;
		    
		return oResp.dwResult;
	}
		
}
}
}
}

#endif /* HELLOWORLDAO_STUB_4_WEB_H */
